﻿using CPPEI.Coolzon.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    /// <summary>
    /// DataLabel.xaml 的交互逻辑
    /// </summary>
    [System.ComponentModel.DisplayName("计数板")]
    [XPartIcon("pack://application:,,,/CPPEI.Coolzon.UI.DataPartLib;component/Images/PartIcons/计数版.png")]
    public partial class DataDigitalPlate : DataItemPart
    {
        private ServiceProxy.DataServiceRef.DataResult _dataResult;
        private double _XFontSize;
        private HorizontalAlignment _XTextAlign;
        private VerticalAlignment _XTextVAlign;
        private FontFamily _XFont;
        private System.Windows.FontWeight _XFontWeight;
        private ImageSourceFile _XDigitalImage;
        private string _XFormatString;
        private Thickness _XLableMargin;
        private string _ItemValueString;

        #region XProperties
        /// <summary>
        /// 显示格式
        /// 01 {0:d2}
        /// </summary>
        [Category("其他")]
        [DisplayName("显示格式")]
        [Description("XFormatString")]
        [Editor(typeof(UI.XProperty.Editors.XStringEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XFormatString", "显示格式")]
        public string XFormatString 
        {
            get
            {
                return _XFormatString;
            }
            set
            {
                _XFormatString = value;
                Refresh();
            }
        }

        /// <summary>
        /// 文本边距
        /// </summary>
        [Category("其他")]
        [DisplayName("文本格式化")]
        [Description("XLabelMargin")]
        [Editor(typeof(UI.XProperty.Editors.XThicknessEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XLabelMargin", "文本格式化")]
        public Thickness XLabelMargin 
        {
            get
            {
                return _XLableMargin;
            }
            set
            {
                _XLableMargin = value;
                Refresh();
            }
        }

        /// <summary>
        /// 字体大小
        /// </summary>
        [Category("其他")]
        [DisplayName("字体大小")]
        [Description("XFontSize")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XFontSize", "字体大小")]
        public double XFontSize
        {
            get
            {
                return _XFontSize;
            }
            set
            {
                _XFontSize = value;
                Refresh();
            }
        }

        /// <summary>
        /// 字体问题
        /// </summary>
        //[Category("其他")]
        //[DisplayName("字体水平位置")]
        //[Description("XTextAlign")]
        //[Editor(typeof(UI.XProperty.Editors.XHorizontalAlignmentEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        //[XProperty(XPropertyCategory.Other, "XTextAlign", "字体水平位置")]
        public HorizontalAlignment XTextAlign
        {
            get
            {
                return _XTextAlign;
            }
            set
            {
                _XTextAlign = value;
                Refresh();
            }
        }

        /// <summary>
        /// 字体问题
        /// </summary>
        [Category("其他")]
        [DisplayName("字体垂直位置")]
        [Description("XTextVAlign")]
        [Editor(typeof(UI.XProperty.Editors.XVerticalAlignmentEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XTextVAlign", "字体垂直位置")]
        public VerticalAlignment XTextVAlign
        {
            get
            {
                return _XTextVAlign;
            }
            set
            {
                _XTextVAlign = value;
                Refresh();
            }
        }

        /// <summary>
        /// 字体样式
        /// </summary>
        [Category("其他")]
        [DisplayName("字体")]
        [Description("XFont")]
        [Editor(typeof(UI.XProperty.Editors.XFontFamilyEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XFont", "字体")]
        public FontFamily XFont
        {
            get
            {
                return _XFont;
            }
            set
            {
                _XFont = value;
                Refresh();
            }
        }
        [Category("其他")]
        [DisplayName("字体粗细")]
        [Description("XFontWeight")]
        [Editor(typeof(UI.XProperty.Editors.XFontWeightEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XFontWeight", "字体粗细")]
        public FontWeight XFontWeight
        {
            get
            {
                return _XFontWeight;
            }
            set
            {
                _XFontWeight = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("计数背景")]
        [Description("XDigitalImage")]
        [Editor(typeof(UI.XProperty.Editors.XImageSourceFileEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "XDigitalImage", "计数背景")]
        public ImageSourceFile XDigitalImage
        {
            get
            {
                return _XDigitalImage;
            }
            set
            {
                _XDigitalImage = value;
                Refresh();
            }
        }
        #endregion

        #region Constructor

        public DataDigitalPlate()
        {
            InitializeComponent();
        }
        protected override void OnApplyXProperty()
        {
            base.OnApplyXProperty();

            if (gdLayout == null) return;
            if (XDataSource == null)
            {
                OriginalInit();
            }

            if (!string.IsNullOrWhiteSpace(_ItemValueString))
            {
                gdLayout.Children.Clear();
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                for (int i = 0; i < _ItemValueString.Length; i++)
                {
                    char digital = _ItemValueString[i];
                    Label lb = new Label();
                    InitControl(lb);
                    TextBlock tb = new TextBlock();
                    tb.Text = digital.ToString();
                    ImageBrush imageBrush = new ImageBrush(XDigitalImage==null? null : XDigitalImage.ImageSource);
                    imageBrush.Stretch = Stretch.Fill;
                    tb.Background = imageBrush;
                    
                    lb.Content = tb;
                    lb.Margin = XLabelMargin;
                    sp.Children.Add(lb);
                }
                gdLayout.Children.Add(sp);
            }
        }
        private void OriginalInit()
        {
            _ItemValueString = "100.96";
        }
        protected override void OnActive(PartContext context)
        {
            base.OnActive(context);
        }
        protected override void OnDeactive(PartContext context)
        {
            gdLayout.Children.Clear();
            base.OnDeactive(context);
        }

        protected override void OnDestory()
        {
            gdLayout.Children.Clear();
            base.OnDestory();
        }

        protected override void OnDataBinding(ServiceProxy.DataServiceRef.DataResult dataResult)
        {
            _dataResult = dataResult;
            base.OnDataBinding(dataResult);
        }
        protected override void OnDataItemBinding(ServiceProxy.DataServiceRef.DataItem dataItem)
        {
            gdLayout.Children.Clear();

            if (!string.IsNullOrEmpty(XFormatString))
            {
                // 格式化处理结果
                if (XFormatString.Contains(":d"))
                {
                    int intValue = 0;
                    if (int.TryParse(dataItem.Value, out intValue))
                    {
                        _ItemValueString = string.Format(XFormatString, intValue);
                    }
                    else
                    {
                        _ItemValueString = dataItem.Value;
                    }
                }
            }
            else
            {
                _ItemValueString = dataItem.Value;
            }

            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;
            for (int i = 0; i < _ItemValueString.Length; i++)
            {
                char digital = _ItemValueString[i];
                Label lb = new Label();
                InitControl(lb);
                ImageBrush imageBrush = new ImageBrush(XDigitalImage.ImageSource);
                imageBrush.Stretch = Stretch.Fill;
                lb.Background = imageBrush;
                lb.Content = digital.ToString();
                lb.Margin = XLabelMargin;
                sp.Children.Add(lb);
            }
            gdLayout.Children.Add(sp);
        }
        
        /// <summary>
        /// 初始化控件属性
        /// </summary>
        private void InitControl(Label lb)
        {
            if (XFontSize != 0)
            {
                lb.FontSize = XFontSize;
            }
            lb.HorizontalContentAlignment = XTextAlign;
            lb.VerticalContentAlignment = XTextVAlign;
            if (XFont != null)
            {
                FontFamilyConverter ffConverter = new FontFamilyConverter();
                lb.FontFamily = XFont;
            }
            if (XFontWeight != null)
            {
                lb.FontWeight = XFontWeight;
            }
            if (XForeground != null)
            {
                lb.Foreground = XForeground.Brush;
            }
            if (XLabelMargin != null)
            {
                lb.Margin = XLabelMargin;
            }
        }
        #endregion
    }
}
